<script lang="ts" setup>
import { ElMessage } from 'element-plus';
import { ref, reactive, onMounted, computed } from 'vue';
import { ArrowLeft } from '@element-plus/icons-vue'
import { useRouter } from 'vue-router';
import type { chapterForm, pointForm, questionForm } from '@/api/type';
import { getChapterDetail, getChapterQuestions } from '@/api/course';
let props = defineProps(["id", 'cid']);
const questions = ref<questionForm[]>([]); // 从后端获取的题目列表
const points = ref<pointForm[]>([])
const getQuestionsPoints = async () => {
    let res = await getChapterQuestions(props.id)
    questions.value = res.data
    console.log(questions.value);

    let result = await getChapterQuestions(props.id)
    points.value = result.data
    console.log(points.value);
}
let showPoints = computed({
    get() {
        return points.value.map(point => point.name).join(" ");
    },
    set() {

    }
})

const submitAnswer = (questionId: number) => {
    const question = questions.value.find(item => item.id === questionId);
    if (question) {
        const selectedOption = question.select;
        let answer = '';
        switch (selectedOption) {
            case 1:
                answer = 'A';
                break;
            case 2:
                answer = 'B';
                break;
            case 3:
                answer = 'C';
                break;
            case 4:
                answer = 'D';
                break;
        }
        if (answer === question.answer) {
            ElMessage.success('回答正确！');

        } else {
            ElMessage.error('回答错误！');
        }
    } else {
        console.log("没存到数据");

    }
};
const router = useRouter();
const back = () => {
    // useCourseStore().setActiveName("second")
    router.push({
        name: 'CourseView',
        query: {
            cid: props.cid
        }
    })
}
// 获取章节信息
const thisChapter = ref<chapterForm>({})
const getDetail = async () => {
    let res = await getChapterDetail(Number(props.id))
    thisChapter.value = res.data
    console.log("thisChapter", thisChapter.value);
}

onMounted(() => {
    getQuestionsPoints();
    getDetail();
})

</script>
<template>
    <!-- 头部 -->
    <el-page-header :icon="ArrowLeft" @click="back" style="width: 100%;">
        <template #content>
            <span> 章节名称：集合论 </span>
            <span class="font" style="margin-left: 40px;margin-right: 20px;">
                学习知识点:{{ showPoints }}
            </span>
        </template>
    </el-page-header>

    <el-divider style="border-style: dotted;" />
    <!-- 主体内容 -->
    <div class="contain">
        <!-- 视频和习题 -->
        <div class="main">
            <video :autoplay="false" controls="true" loop style="width: 70%;height: 100%;" :src="thisChapter.video">
                <source :src="thisChapter.video" type="video/mp4">
            </video>
            <div class="testBody">
                <el-carousel :autoplay="false" indicator-position="outside" trigger="click">
                    <el-carousel-item v-for="(item) in questions" :key="item.id">
                        <div class="test">
                            <h4>{{ item.content }}</h4>
                            <el-radio-group v-model="item.select" style="max-width: 80%;">
                                <el-radio :value="1">A. {{ item.option1 }}</el-radio>
                                <el-radio :value="2">B. {{ item.option2 }}</el-radio>
                                <el-radio :value="3">C. {{ item.option3 }}</el-radio>
                                <el-radio :value="4">D. {{ item.option4 }}</el-radio>
                            </el-radio-group>
                            <el-button @click="() => submitAnswer(item.id as number)">提交</el-button>
                        </div>
                    </el-carousel-item>
                </el-carousel>
            </div>
        </div>
    </div>

</template>

<style scoped>
.font {
    color: var(--el-text-color-regular);
    font-size: 15px;
}

.contain {
    display: flex;
    justify-content: center;
    align-items: center;

    .main {
        width: 70%;
        display: flex;
        justify-content: center;
        align-items: center;
        flex-direction: column;
        flex-wrap: wrap;

    }

    .testBody {
        width: 70%;
        margin: 30px 0px;
    }

    .el-carousel {
        background-color: #d3dce6;
    }

    .el-carousel__item h3 {
        color: #475669;
        opacity: 0.75;
        line-height: 300px;
        margin: 0;
        text-align: center;
    }

    .test {
        display: flex;
        justify-content: center;
        align-items: center;
        flex-direction: column;
        flex-wrap: wrap;
        height: 100%;
        /* background: cornsilk; */
    }
}
</style>
